package com.qsmx.store.app.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import androidx.annotation.Nullable;

import com.qsmx.store.app.R;
import com.qsmx.store.app.databinding.DelegateNetTestBinding;
import com.qsmx.store.app.fusion.FusionCode;
import com.qsmx.store.core.fusion.FusionPre;
import com.qsmx.store.app.main.adapter.AppTypeSpanAdapter;
import com.qsmx.store.app.util.DialogUtil;
import com.qsmx.store.app.util.PingUtil;
import com.qsmx.store.app.util.ScanDeviceUtile;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.util.StringUtil;
import com.qsmx.store.core.util.loader.AtmosLoader;
import com.qsmx.store.core.util.storage.AtmosPreference;
import com.qsmx.store.core.webfile.WebViewDelegate;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class NetTestDelegate extends AtmosDelegate {

    private List<String> mAllIps = new ArrayList<>();
    private List<String> mUrlList;
    private DelegateNetTestBinding mBinding;

    @Override
    public Object setLayout() {
        return R.layout.delegate_net_test;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mUrlList = new ArrayList<>();
        mBinding = DelegateNetTestBinding.bind(rootView);

        initAddress();
        mBinding.tvPing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtil.isNotEmpty(Objects.requireNonNull(mBinding.etAddress.getText()).toString())) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            getProxyActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mBinding.tvOutInfo.setText(PingUtil.ping(PingUtil.createSimplePingCommand(1, 1, mBinding.etAddress.getText().toString())));
                                }
                            });
                        }
                    }).start();
                } else {
                    DialogUtil.showErrorDialog(getContext(), "请输入地址", "确定");
                }
            }
        });

        mBinding.tvOpenInWv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtil.isNotEmpty(Objects.requireNonNull(mBinding.etAddress.getText()).toString())) {
                    if (AtmosPreference.getCustomStringPre(FusionPre.APP_ELM_INFO).equals("测试")) {
                        getSupportDelegate().start(WebViewDelegate.create("http://" + mBinding.etAddress.getText().toString(), "test", false));
                    } else {
                        getSupportDelegate().start(WebViewDelegate.create("https://" + mBinding.etAddress.getText().toString(), "test", false));
                    }
                } else {
                    DialogUtil.showErrorDialog(getContext(), "请输入地址", "确定");
                }
            }
        });

        mBinding.tvOpenInExport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (StringUtil.isNotEmpty(Objects.requireNonNull(mBinding.etAddress.getText()).toString())) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    if (AtmosPreference.getCustomStringPre(FusionPre.APP_ELM_INFO).equals("测试")) {
                        intent.setData(Uri.parse("http://" + mBinding.etAddress.getText().toString()));
                    } else {
                        intent.setData(Uri.parse("https://" + mBinding.etAddress.getText().toString()));
                    }
                    startActivity(intent);
                } else {
                    DialogUtil.showErrorDialog(getContext(), "请输入地址", "确定");
                }
            }
        });

        mBinding.tvScanIp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scanAllIp();
            }
        });

        mBinding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSupportDelegate().pop();
            }
        });
    }

    private void scanAllIp() {
        AtmosLoader.showLoading(getContext());
        new Thread(new Runnable() {
            @Override
            public void run() {
                mAllIps.clear();
                mAllIps = ScanDeviceUtile.scan();
                getProxyActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        AtmosLoader.stopLoading();
                        if (mAllIps != null) {
                            for (String ip : mAllIps) {
                                mBinding.tvOutInfo.append(ip + "\n");
                            }
                        } else {
                            DialogUtil.showErrorDialog(getContext(), "扫描失败", "确定");
                        }
                    }
                });
            }
        }).start();
    }

    private void initAddress() {
        if (AtmosPreference.getCustomStringPre(FusionPre.APP_ELM_INFO).equals("正式")) {
            mUrlList.add(FusionCode.Address_Release.BAIDU);
            mUrlList.add(FusionCode.Address_Release.APP_QSMX);
            mUrlList.add(FusionCode.Address_Release.APPLET_QSMX);
            mUrlList.add(FusionCode.Address_Release.ENET);
            mUrlList.add("选择地址");
        } else {
            mUrlList.add(FusionCode.Address_Dev.BAIDU);
            mUrlList.add(FusionCode.Address_Dev.APP_QSMX);
            mUrlList.add(FusionCode.Address_Dev.APPLET_QSMX);
            mUrlList.add(FusionCode.Address_Dev.ENET);
            mUrlList.add("选择地址");
        }

        initUrlSpanInfo();
    }

    private void initUrlSpanInfo() {

        //适配器
        AppTypeSpanAdapter<String> arrAdapter = new AppTypeSpanAdapter<String>(requireContext(), R.layout.simple_spinner_item, mUrlList);
        //设置样式
        arrAdapter.setDropDownViewResource(R.layout.dropdown_stytle);
        //加载适配器
        mBinding.spAppUrl.setAdapter(arrAdapter);
        mBinding.spAppUrl.setSelection(mUrlList.size() - 1, true);
        mBinding.spAppUrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                if (position == mUrlList.size() - 1) {
                    return;
                }
                mBinding.etAddress.setText(mUrlList.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }
}
